#!/bin/sh

APPNAME=`awk -F '_' '{print tolower($1)}' vsn.mk`

# Versians of the installed components - erts, kernel etc.
ERLDIR=`erl -noshell -eval 'io:format("~s",[code:root_dir()]).' -s erlang halt`
ls -1 ${ERLDIR}/ | egrep 'erts-' | awk -F'-' '{printf "%s_VSN=%s\n", toupper($1),$2}' > system.mk
ls -1 ${ERLDIR}/lib/ | egrep 'kernel-|stdlib-|sasl-|mnesia-' | awk -F'-' '{printf "%s_VSN=%s\n", toupper($1),$2}' >> system.mk
. ./vsn.mk
. ./system.mk

sed -e "s;%KERNEL_VSN%;${KERNEL_VSN};" \
    -e "s;%STDLIB_VSN%;${STDLIB_VSN};" \
    -e "s;%SASL_VSN%;${SASL_VSN};" \
    -e "s;%MNESIA_VSN%;${MNESIA_VSN};" \
    -e "s;%ERTS_VSN%;${ERTS_VSN};" \
    -e "s;%REVERL_VSN%;${REVERL_VSN};" < src/${APPNAME}.rel.src > ebin/${APPNAME}.rel
sed -e "s;%REVERL_VSN%;${REVERL_VSN};" < src/${APPNAME}.app.src > ebin/${APPNAME}.app

erlc -o ebin -pa ebin ebin/${APPNAME}.rel
